
By : Nanda Kishor K N                                      Mail ID : nandakishorkn@rediffmail.com
_________________________________________________________________________________________________

                                       C For Swimmers
                                       ______________

      If u don't know how to swim, learn it now. Otherwise participate in the competition.

_________________________________________________________________________________________________

		                 Topic : Programming Problems (Part II)
                		 _____   ____________________

*************************************************************************************************
NOTE : * Use the necessary header files.
       * Use comments to explain the underlying logic of various program features.
       * Use of spacing, indentation, etc. is strongly encouraged as a matter of good programming
	 practice.
       * Enter, compile and execute the C programs. Also verify that they run correctly with your
	 particular version of C. (If any of the programs do not run, try to determine why.)
*************************************************************************************************


[Q001]. Write a program (W.A.P.) in C to clear the screen and print C FOR SWIMMERS on each line,
forming a diagonal pattern running from upper-left to lower right.[Use suitable delay]

Ans.

/* Print C FOR SWIMMERS on each line, forming a diagonal pattern */
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
   char str[]="C For Swimmers";
   int x=1,y;
   for (y=1; y<=25; y++)
      {
         clrscr();
         gotoxy(x,y);
         printf("%s",str);
         delay(250);
         if (y < 8)
            x+=2;
         else
            x+=3;
      }
} /* End of main */
_________________________________________________________________________________________________
	
*[Q002]. W.A.P. in C to SORT the array of strings based on first 3-LETTERS ONLY.
_________________________________________________________________________________________________

[Q003]. Some C Functions take variable argument list in addition to taking a number of fixed
(known) parameters. Implement using USER-DEFINED C FUNCTION that take VARIABLE ARGUMENT LIST and
COMPUTE THE SUM OF VALUES specified in the list.
Ex: int Sum(int a,...)  // Here ... means VARIABLE ARGUMENT LIST
    {
       // Your Code Goes Here to access the values in the variable argument list
    }
    void main(void)
    {
      printf("%d",Sum(5,6,7,8,9,10)); // Prints the RESULT (Sum of these values=45)
    }

Ans.

/* Compute the sum of values in the variable argument list */
#include<stdio.h>
#include<stdarg.h>
int Sum(int a,...)
{
   int total=a;
   va_list ap;
   int arg;
   va_start(ap,a);
   while ((arg = va_arg(ap,int)) != 0)
      {
         total+=arg;
      }
   va_end(ap);
   return total;
}
void main()
{
   clrscr();
   printf("%d",Sum(5,6,7,8,9,10));
   getch();
} /* End of main */
_________________________________________________________________________________________________
	
[Q004]. W.A.P. in C to compute the sum of two values using function that takes two arguments
(INTEGERS) and IT SHOULD RETURN the sum WITHOUT USING the RETURN statement i.e return type of the
function is INTEGER ('int' data type).
[NOTE : DO NOT MAKE USE OF ANY GLOBAL VARIABLES OR POINTER CONCEPT]

Ans. 

// Compute the sum of two values using function and return the result w/o using RETURN statement
#include<stdio.h>
#include<conio.h>
int Sum(int a,int b)
{
  _AX = a + b; // equivalent to : return (a + b);
}
void main(void)
{
  clrscr();
  printf("Result is = %d",Sum(4,6));
  getch();
} /* End of main */
_________________________________________________________________________________________________

[Q005]. W.A.P. in C to SWAP the contents of TWO VARIABLES WITHOUT using ASSIGNMENT OPERATOR.
[HINT : USE 'asm' statement]

/* To swap the contents of two variables using 'asm' statement */
#include<stdio.h>
#include<conio.h>
void main()
{
  int val=85,num=77;
  clrscr();
  printf("Before swapping : %d and %d",val,num);
  asm mov ax,val
  asm mov cx,num
  asm mov num,ax
  asm mov val,cx
  printf("\n\nAfter swapping  : %d and %d",val,num);
} /* End of main */
_________________________________________________________________________________________________

[Q006]. W.A.P. in C to FIND THE LARGEST of two numbers (integer) WITHOUT using ?: operator, if,
if...else and switch statements.
[HINT : USE 'asm' statement]

/* To find largest of two numbers using 'asm' statement */
#include<stdio.h>
#include<conio.h>
void main()
{
  int val=85,num=77;
  clrscr();
  asm mov ax,val
  asm cmp ax,num
  asm jg FirstNum
  asm mov ax,num
FirstNum:printf("Largest of two numbers is %d",_AX);
} /* End of main */
_________________________________________________________________________________________________
	
[Q007]. W.A.P. in C to print the following output WITHOUT USING GOTO STATEMENT, CONDITIONAL
STATEMENTS (if, if...else & switch statements) and ANY LOOP STATEMENTS (for, while & do..while) :

(a)
*****
****
***
**
*

/* To print the above pattern or output using asm statement */
#include<stdio.h>
#include<conio.h>
void main(void)
{
	int i=5,j;
	clrscr();
	// asm statement doesn't end with semicolon (starts with 'asm' keyword)
	asm mov ax,i	// Move the value of 'i' to accumulator (ax)	
Loopi:  j=i;
Loopj:	printf("*");    // Print asterisk (*)
	j=j-1;
	asm mov cx,j	// Decrement the counter : Inner Loop
	asm cmp cx,0
	asm jnz Loopj	// Terminate the Inner Loop if counter is 0, otherwise goto Loopj
	i=i-1;
	printf("\n");	
	asm mov ax,i	// Decrement the counter : Outer Loop
	asm cmp ax,0
	asm jnz Loopi	// Terminate the Outer Loop if counter is 0, otherwise goto Loopi
	getch();
} /* End of main */


(b)
*
**
***
****
*****

/* To print the above pattern or output using asm statement */
#include<stdio.h>
#include<conio.h>
void main(void)
{
	int i=1,j;
	clrscr();

	// asm statement doesn't end with semicolon (starts with 'asm' keyword)
	asm mov ax,i	// Move the value of 'i' to accumulator (ax)	
Loopi:j=0;
Loopj:printf("*");	// Print asterisk (*)
	j=j+1;
	asm mov cx,j	// Increment the counter : Inner Loop
	asm cmp cx,i	
	asm jnz Loopj	// Terminate the Inner Loop if Inner Loop counter = Outer Loop counter
	i=i+1;
	printf("\n");
	asm mov ax,i	// Increment the counter : Outer Loop
	asm cmp ax,6
	asm jnz Loopi	// Terminate the Outer Loop if counter = 6

	getch();
} /* End of main */

[HINT : USE 'asm' statement]
_________________________________________________________________________________________________

[Q008]. W.A.P. in C to REVERSE THE WORDS IN A GIVEN LINE OF TEXT.
[HINT : USE Built-in function 'strtok' in 'string.h' header file]

Ans.

/* To reverse the words in a given line of text */
#include<stdio.h>
#include<string.h>
void main()
{
   char ch,str[80],*res="",*temp,*temp1;
   clrscr();
   printf("Enter a sentence :\n");
   scanf("%[^\n]",str);
   temp=strtok(str," ");
   do
   {
      strcpy(temp1,temp);
      strcat(temp1," ");
      strcat(temp1,res);
      strcpy(res,temp1);
      temp=strtok(NULL," ");
   }while (temp != NULL);

   printf("\n Result : %s",res);
   getch();
} /* End of main */
_________________________________________________________________________________________________
	
*[Q009]. W.A.P. in C to CONVERT THE TEXT within /* ... */ to UPPER-CASE (all occurrences) from the
SPECIFIED C Source Code File and save the file without altering the remaining text. 
_________________________________________________________________________________________________

[Q010]. W.A.P. in C to READ a line of text and WRITE it out BACKWARDS using RECURSIVE Function. 

Ans.

/* To reverse a given line of test using recursive function */
#include<stdio.h>
#include<conio.h>
void reverse(void)
{
  char c;
  if ((c = getchar()) != '\n')
     reverse();
  putchar(c);
  return;
}
void main()
{
  clrscr();
  printf("Please enter a line of text below\n");
  reverse();
} /* End of main */
_________________________________________________________________________________________________
NOTE : *[Qxxx] means Releasing Soon
				Thanx for using "C For Swimmers".
Regarding this material, you can send Bug Reports, Suggestions, Comments,etc. to nandakishorkn@rediffmail.com